#!/bin/bash

PREDICT_PLATFORM="x86"
# PREDICT_PLATFORM="arm64"

check_android_ndk() {
  if [[ "$ANDROID_NDK" == "" ]]; then
    echo "Please set ANDROID_NDK in enviroment variable, for example: export ANDROID_NDK=/root/usr/android-ndk-r18b/"
    exit
  fi
}

core=$(< /proc/cpuinfo grep -c "processor")
rm -rf build
mkdir build
cd build

if [[ "X$PREDICT_PLATFORM" == "Xx86" ]]; then
  cmake -DCMAKE_BUILD_TYPE="Release" -DENABLE_ARM=off ..
else
  check_android_ndk
  cmake -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake"                    \
        -DANDROID_NATIVE_API_LEVEL="19" -DANDROID_NDK="${ANDROID_NDK}" -DANDROID_ABI="arm64-v8a" \
        -DANDROID_TOOLCHAIN_NAME="aarch64-linux-android-clang" -DANDROID_STL="c++_static"        \
        -DCMAKE_BUILD_TYPE="Release" -DENABLE_ARM=on ..
fi
make VERBOSE=2 -j${core}